343E - Pumping Stations - CodeForces Solution


brute force dfs and similar divide and conquer flows graphs greedy trees *2900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 205, B = 2005, I = 1e9;
int s, t, n, m, as[N], sum;
struct flow 
{
    int h[N], tot = 1, d[N], cur[N], a[N]; 
    queue<int> q; 
    struct edge 
    {
        int to, val, nxt; 
    }e[B << 1];
    void add(int u, int v, int w) 
    {
        e[++tot] = {v, w, h[u]}; h[u] = tot; 
        e[++tot] = {u, 0, h[v]}; h[v] = tot;
    }
    bool bfs() 
    {
        while(q.size()) q.pop();
        for(int i = 1; i <= n; i++) cur[i] = h[i], d[i] = 0;
        q.push(s); d[s] = 1;
        while(q.size()) 
        {
            int u = q.front(); q.pop();
            for(int i = h[u]; i; i = e[i].nxt)
            {
                int v = e[i].to; 
                if(e[i].val && !d[v]) 
                {
                    d[v] = d[u] + 1;
                    if(v == t) return 1;
                    q.push(v);
                }
            }
        }
        return 0;
    }
    int dfs(int u, int sum) 
    {
        if(u == t) return sum;
        int res = sum; 
        for(int &i = cur[u]; i; i = e[i].nxt)
        {
            int v = e[i].to, w = e[i].val;
            if(w == 0 || d[v] != d[u] + 1) continue;
            int k = dfs(v, min(res, w)); 
            if(k == 0) d[v] = 0;
            else 
            {
                e[i].val -= k, e[i ^ 1].val += k; res -= k;
                if(res == 0) break;
            }
        }
        return sum - res;
    }
    void undo() 
    {
        for(int i = 2; i <= tot; i += 2)
        {
            e[i].val += e[i ^ 1].val;
            e[i ^ 1].val = 0;
        }
    }
    int dinic() 
    {
        int sum = 0; undo();
        while(bfs()) sum += dfs(s, I);
        return sum; 
    }
    int ta[N], fa[N], ct;
    vector<int> g[N];
    struct node 
    {
        int u, v, w; 
        friend bool operator < (node a, node b) {return a.w > b.w;}
    }ed[N];
    int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
    void merge(int x, int y) 
    {
        x = find(x), y = find(y);
        if(x == y) return;
        fa[x] = y; g[y].insert(begin(g[y]), begin(g[x]), end(g[x]));
    }
    void solve(int l, int r) 
    {
        if(l == r) return;
        s = a[l], t = a[r]; ed[++ct] = {s, t, dinic()};
        int na = 0, mid = l - 1;
        for(int i = l; i <= r; i++) 
        {
            if(d[a[i]]) a[++mid] = a[i];
            else ta[++na] = a[i]; 
        }
        for(int i = 1; i <= na; i++) a[i + mid] = ta[i];
        solve(l, mid); solve(mid + 1, r);
    }   
    void solve() 
    {
        for(int i = 1; i <= n; i++) a[i] = i, fa[i] = i, g[i].push_back(i);
        solve(1, n); sort(ed + 1, ed + ct + 1);
        for(int i = 1; i <= ct; i++) 
        {
            sum += ed[i].w;
            merge(ed[i].u, ed[i].v);
        }
        int u = find(1);
        for(int i = 1; i <= n; i++) as[i] = g[u][i - 1];
    }
}f;
int u, v, w;
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        f.add(u, v, w), f.add(v, u, w);
    }
    f.solve();
    printf("%d\n", sum);
    for(int i = 1; i <= n; i++) printf("%d ", as[i]);
    printf("\n");
    return 0;
}


Comments

Submit
0 Comments
More Questions

43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos